#include "LoginSignup.h"
#include "StdFile.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define true 1
#define false 0

struct User *CreaterUser1(char *name, char *pwd)
{
    struct User *user = (struct User *)malloc(sizeof(struct User));
    if (user == NULL)
    {
        printf("CreateUser malloc error!\n");
        return NULL;
    }
    user->name = name;
    user->password = pwd;
    return user;
}

int IsEqualID(const char *FilePath, char *userName)
{
    FILE *file = fopen(FilePath, "r");
    if (file == NULL)
    {
        printf("fopen error!\n");
        return false;
    }
    char line[15]; //
    while (fgets(line, sizeof(line), file))
    {
        char *name = strtok(line, "=");
        if (name != NULL && strcmp(name, userName) == 0)
        {
            fclose(file);
            return true;
        }
        memset(line, 0, 15);
    }
    fclose(file);
    return false;
}
int checkid(char *name, char *pwd)
{
    // char ID[100] = {0};
    // char PWD[100] = {0};
    if (IsEqualID("UserInformation.txt", name))
    {
        return false;
    }
    else
    {
        return true;
    }
}
int SignUp(char *name, char *pwd)
{
    int i = checkid(name, pwd);

    if (i != true)
    {
        char account[100] = {0};
        char passwd[100] = {0};
        while (i != true)
        {

            printf("账户重复请再次输入\n");
            printf("输入账号:\n");
            scanf("%s", account);
            printf("输入密码:\n");
            scanf("%s", passwd);
            i = checkid(account, passwd);
        }

        int len = strlen(account) + strlen(passwd) + 1 + 1;
        char *userdata = NULL;
        userdata = (char *)malloc(sizeof(char) * len);
        if (userdata == NULL)
        {
            printf("userdata malloc error!\n");
            return false;
        }
        strcpy(userdata, account);
        strcat(userdata, "=");
        strcat(userdata, passwd);
        // printf("%s\n", userdata);
        AppendToFile("UserInformation.txt", userdata, len - 1); // StdFile.c文件
        printf("注册成功！\n");
        free(userdata);
        return true;
    }
    if (i == true)
    {
        int len = strlen(name) + strlen(pwd) + 1 + 1;
        char *userdata = NULL;
        userdata = (char *)malloc(sizeof(char) * len);
        if (userdata == NULL)
        {
            printf("userdata malloc error!\n");
            return false;
        }
        strcpy(userdata, name);
        strcat(userdata, "=");
        strcat(userdata, pwd);
        // printf("%s\n", userdata);
        AppendToFile("UserInformation.txt", userdata, len - 1); // StdFile.c文件
        printf("注册成功！\n");
        free(userdata);
        return true;
    }
}

int Login(char *name, char *pwd, DLlist *list)
{
    struct Node *TravelPoint = list->head;
    while (TravelPoint != NULL)
    {
        struct User *user = (struct User *)TravelPoint->data;
        if (strcmp(user->name, name) == 0 && strcmp(user->password, pwd) == 0)
        {
            printf("欢迎用户%s进入魔塔!\n", name);
            sleep(1);
            system("clear");
            return true;
        }
        TravelPoint = TravelPoint->next;
    }
    printf("用户或密码错误!\n");
    return false;
}
void StrTok(ElementType element, DLlist *list)
{
    char *str = (char *)element;
    char *UserName = strtok(str, "=");
    char *pwd = strtok(NULL, "=");
    struct User *user = CreaterUser1(UserName, pwd);
    InsertTail(list, user);
}

char *start(DLlist *GameUserData)
{
    printf("1.注册\n");
    printf("2.登录\n");
    int choice = 0;
    scanf("%d", &choice);

    switch (choice)
    {
    case 1:
        char account[100] = {0};
        char passwd[100] = {0};
        printf("输入注册账号:\n");
        scanf("%s", account);
        printf("输入密码:\n");
        scanf("%s", passwd);
        SignUp(account, passwd);

    case 2:
        // char account1[100] = {0};
        char *account1 = (char *)malloc(sizeof(char) * 100);
        char passwd1[100] = {0};
        while (checkid(account1, passwd1) != false)
        {

            printf("输入登录账号:\n");
            scanf("%s", account1);
            printf("输入密码:\n");
            scanf("%s", passwd1);
            checkid(account1, passwd1);
        }
        DLlist *list = GetLineFromFile("UserInformation.txt");
        Trave(list, StrTok, GameUserData);
        while (Login(account1, passwd1, GameUserData) == true)
        {

            return account1;
        }
    }
}

void start2()
{
    printf("*******************************************\n");
    printf("*        1        继续游戏                *\n");
    printf("*        2        重新游戏                *\n");
    printf("*******************************************\n");
    printf("请输入1或2:\n");
}